var express = require("express");
var router = express.Router();
var jwt = require("../utils/jwt");

let users = [
  {
    loginId: "jxx",
    loginPwd: 123,
  },
];
// 登录
router.post("/login", function (req, res) {
  const { loginId, loginPwd } = req.query;
  for (let i = 0; i < users.length; i++) {
    let user = Object.values(users[i]);
    if (loginId === user[0] && Number(loginPwd) === user[1]) {
      const token = jwt.generate({ loginId }, "8h");
      res.header("Authorization", token);
      res.send({ ok: 1 });
      return;
    }
  }
  res.send({ ok: 0 });
});
// 修改密码
router.post("/changePwd", function (req, res) {
  const { loginId, password, newPassword } = req.body;

  for (let i = 0; i < users.length; i++) {
    let user = Object.values(users[i]);
    if (loginId === user[0] && Number(password) === user[1]) {
      users[i]["loginPwd"] = Number(newPassword);
      console.log(users[i]["loginPwd"]);
      res.send({ ok: 1 });

      return;
    }
  }
  res.send({ ok: 0 });
});
// 注册
router.post("/register", function (req, res) {
  let { newLoginId, newLoginPwd } = req.body;
  newLoginPwd = Number(newLoginPwd);
  users.push({ newLoginId, newLoginPwd });
  res.send({ ok: 1 });
});

module.exports = router;
